package com.mf.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mf.bo.PositionsDetailBo;
import com.mf.entity.Positions;
import com.mf.mapper.PositionsMapper;
import com.mf.service.IPositionsService;
import com.mf.vo.positions.PositionsDetailVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mf
 * @since 2024-06-08
 */
@Service
public class PositionsServiceImpl extends ServiceImpl<PositionsMapper, Positions> implements IPositionsService {
    @Resource
    private PositionsMapper positionsMapper;

    /**
     * 根据股票代码查询持股数量
     */
    @Override
    @Transactional
    public Integer findPositionsByDm(String dm) {
        return positionsMapper.findPositionsByDm(dm);
    }

    /**
     * 查询条件下的持仓详情
     *
     * @param positionsDetailBo 查询条件
     * @return 持仓详情
     */
    @Override
    @Transactional
    public List<Positions> findPositionsDetail(PositionsDetailBo positionsDetailBo) {
        List<Positions> positionsList = positionsMapper.findPositionsDetail(positionsDetailBo);
        final Map<String, Positions> collect = positionsList.stream()
                .collect(Collectors.toMap(
                        Positions::getDm, // keyMapper - 提取用于分组的键
                        Function.identity(), // valueMapper - 直接使用流中的元素作为值
                        Positions::merge // mergeFunction - 合并两个值的函数
                ));
        return new java.util.ArrayList<>(collect.values().stream().toList());
    }


}
